﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.ComponentModel;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
//using Microsoft.ManagementConsole;


namespace TimNilimaaConsulting.DeploymentWeb
{
    public class SCCM_Connector
    {

        public static string GetFQDN()
        {
            string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string hostName = System.Net.Dns.GetHostName();
            string fqdn = "";
            if (!hostName.Contains(domainName))
                fqdn = hostName + "." + domainName;
            else
                fqdn = hostName;

            return fqdn;
        }


        public WqlConnectionManager Connect(string servername)
        {
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager();
            connection.Context.Add("ApplicationName", String.Format("{0} DeploymentWeb", System.Configuration.ConfigurationManager.AppSettings["OrganizationName"]));
            connection.Context.Add("MachineName", GetFQDN());

            connection.Connect(servername);

            return connection;
        }
        public WqlConnectionManager Connect (string servername, string username, string password)
        {
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager();
            connection.Context.Add("ApplicationName", String.Format("{0} DeploymentWeb", System.Configuration.ConfigurationManager.AppSettings["OrganizationName"]));
            connection.Context.Add("MachineName", GetFQDN());

            connection.Connect(servername, username, password);

            return connection;
        }
    }
}